//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package admin_roadmap_api_serv

import (
	"context"
	"time"

	"atomgit.com/openlinksaas-org/api-server/admin_api_serv/admin_common"
	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/roadmap_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/roadmap_api"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *RoadmapAdminApiImpl) AddTag(ctx context.Context, req *roadmap_api.AdminAddTagRequest) (*roadmap_api.AdminAddTagResponse, error) {
	adminSess, ok := dao.CacheDao.GetAdminSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_api.AdminAddTagResponse{
			Code:   roadmap_api.AdminAddTagResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !adminSess.AuthOk {
		return &roadmap_api.AdminAddTagResponse{
			Code:   roadmap_api.AdminAddTagResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, adminSess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.RoadmapPerm.AddTag {
		return &roadmap_api.AdminAddTagResponse{
			Code:   roadmap_api.AdminAddTagResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = roadmap_dao.TagDao.Insert(sessCtx, &roadmap_dao.TagInfoDbItem{
			Tag:       req.Tag,
			Name:      req.Name,
			TimeStamp: nowTime,
		})
		if err != nil {
			return nil, err
		}
		return &roadmap_api.AdminAddTagResponse{
			Code: roadmap_api.AdminAddTagResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_api.AdminAddTagResponse), nil
}

func (apiImpl *RoadmapAdminApiImpl) ModifyTag(ctx context.Context, req *roadmap_api.AdminModifyTagRequest) (*roadmap_api.AdminModifyTagResponse, error) {
	adminSess, ok := dao.CacheDao.GetAdminSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_api.AdminModifyTagResponse{
			Code:   roadmap_api.AdminModifyTagResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !adminSess.AuthOk {
		return &roadmap_api.AdminModifyTagResponse{
			Code:   roadmap_api.AdminModifyTagResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, adminSess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.RoadmapPerm.ModifyTag {
		return &roadmap_api.AdminModifyTagResponse{
			Code:   roadmap_api.AdminModifyTagResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = roadmap_dao.TagDao.Update(sessCtx, req.Tag, req.Name)
		if err != nil {
			return nil, err
		}
		return &roadmap_api.AdminModifyTagResponse{
			Code: roadmap_api.AdminModifyTagResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_api.AdminModifyTagResponse), nil
}

func (apiImpl *RoadmapAdminApiImpl) RemoveTag(ctx context.Context, req *roadmap_api.AdminRemoveTagRequest) (*roadmap_api.AdminRemoveTagResponse, error) {
	adminSess, ok := dao.CacheDao.GetAdminSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_api.AdminRemoveTagResponse{
			Code:   roadmap_api.AdminRemoveTagResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !adminSess.AuthOk {
		return &roadmap_api.AdminRemoveTagResponse{
			Code:   roadmap_api.AdminRemoveTagResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, adminSess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.RoadmapPerm.RemoveTag {
		return &roadmap_api.AdminRemoveTagResponse{
			Code:   roadmap_api.AdminRemoveTagResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = roadmap_dao.TagDao.Remove(sessCtx, req.Tag)
		if err != nil {
			return nil, err
		}
		return &roadmap_api.AdminRemoveTagResponse{
			Code: roadmap_api.AdminRemoveTagResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_api.AdminRemoveTagResponse), nil
}

func (apiImpl *RoadmapAdminApiImpl) ListTag(ctx context.Context, req *roadmap_api.AdminListTagRequest) (*roadmap_api.AdminListTagResponse, error) {
	emptyList := []*roadmap_api.TagInfo{}

	adminSess, ok := dao.CacheDao.GetAdminSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_api.AdminListTagResponse{
			Code:    roadmap_api.AdminListTagResponse_CODE_WRONG_SESSION,
			ErrMsg:  "会话不存在",
			TagList: emptyList,
		}, nil
	}
	if !adminSess.AuthOk {
		return &roadmap_api.AdminListTagResponse{
			Code:    roadmap_api.AdminListTagResponse_CODE_NOT_AUTH,
			ErrMsg:  "未授权会话",
			TagList: emptyList,
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, adminSess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.RoadmapPerm.Read {
		return &roadmap_api.AdminListTagResponse{
			Code:    roadmap_api.AdminListTagResponse_CODE_NO_PERMISSION,
			ErrMsg:  "没有权限",
			TagList: emptyList,
		}, nil
	}

	tagItemList, err := roadmap_dao.TagDao.List(ctx)
	if err != nil {
		return nil, err
	}
	tagList := []*roadmap_api.TagInfo{}
	for _, tagItem := range tagItemList {
		tagList = append(tagList, tagItem.ToTagInfo())
	}
	return &roadmap_api.AdminListTagResponse{
		Code:    roadmap_api.AdminListTagResponse_CODE_OK,
		TagList: tagList,
	}, nil
}
